package com.woniuxy.goods.controller;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.woniuxy.core.annotation.Permission;
import com.woniuxy.core.exception.ParamException;
import com.woniuxy.core.util.Result;
import com.woniuxy.goods.dto.AttrConfigDto;
import com.woniuxy.goods.dto.AttrGroupDto;
import com.woniuxy.goods.entity.AttributeGroup;
import com.woniuxy.goods.entity.GroupType;
import com.woniuxy.goods.form.AddGroupForm;
import com.woniuxy.goods.form.FindConfigPageForm;
import com.woniuxy.goods.form.UpdateAttrGroupForm;
import com.woniuxy.goods.param.AddGroupParam;
import com.woniuxy.goods.param.FindConfigPageParam;
import com.woniuxy.goods.param.UpdateAttrGroupParam;
import com.woniuxy.goods.service.AttributeGroupService;
import io.swagger.annotations.*;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * <p>
 *  前端控制器
 * </p>
 * 属性组管理控制层接口
 * @author ...
 * @since 2024年05月22日
 */
@RestController
@Api(tags = "属性组管理接口")
@RequestMapping("/attributeGroup")
public class AttributeGroupController {
    /**
     * 属性组服务层接口
     */
    @Resource
    private AttributeGroupService attributeGroupService;

    /**
     * 新增属性组
     * @param form 新增属性组参数对象
     * @return 属性组信息
     * @throws Exception
     */
    @ApiOperation("新增属性组")
    @ApiImplicitParam("新增属性组参数对象")
    @ApiResponses({
            @ApiResponse(code = 200,message = "OK",response = AttrGroupDto.class)
    })
    @PostMapping("/addAttrGroup")
    @Permission(roles = {"超级管理员","管理员"})
    public Result addAttrGroup(@Validated @RequestBody AddGroupForm form, BindingResult br)throws Exception{
        if(br.hasErrors()){
            throw new ParamException(10001,"参数有误");
        }
        AttrGroupDto group = attributeGroupService.addAttrGroup(BeanUtil.toBean(form, AddGroupParam.class));
        return Result.success("新增属性组成功",group);
    }

    /**
     * 删除属性组
     * @param groupId 属性组id
     * @return
     * @throws Exception
     */
    @ApiOperation("删除属性组")
    @ApiImplicitParam("属性组id")
    @GetMapping("/delAttrGroup")
    @Permission(roles = {"超级管理员","管理员"})
    public Result delAttrGroup(Integer groupId)throws Exception{
        attributeGroupService.delAttrGroup(groupId);
        return Result.success("删除属性组成功",null);
    }

    /**
     * 修改属性组
     * @param form 修改属性组对象
     * @return 属性组信息
     * @throws Exception
     */
    @ApiOperation("修改属性组")
    @ApiResponses({
            @ApiResponse(code = 200,message = "OK",response = AttrGroupDto.class)
    })
    @PostMapping("/updateAttrGroup")
    @Permission(roles = {"超级管理员","管理员"})
    public Result updateAttrGroup(@Validated @RequestBody UpdateAttrGroupForm form,BindingResult br)throws Exception{
        if(br.hasErrors()){
            throw new ParamException(10001,"参数有误");
        }
        UpdateAttrGroupParam param = BeanUtil.toBean(form, UpdateAttrGroupParam.class);
        AttrGroupDto group = attributeGroupService.updateAttrGroup(param);
        return Result.success("修改属性组成功",group);
    }

    /**
     * 修改属性组状态
     * @param form 修改属性组对象
     * @return 属性组信息
     * @throws Exception
     */
    @ApiOperation("修改属性组状态")
    @ApiResponses({
            @ApiResponse(code = 200,message = "OK",response = AttrGroupDto.class)
    })
    @PostMapping("/updateAttrGroupByIds")
    @Permission(roles = {"超级管理员","管理员"})
    public Result updateAttrGroupByIds(@Validated @RequestBody UpdateAttrGroupForm form,BindingResult br)throws Exception{
        if(br.hasErrors()) throw  new ParamException(10001,"参数有误");
        UpdateAttrGroupParam param = BeanUtil.toBean(form, UpdateAttrGroupParam.class);
        List<AttrGroupDto> group = attributeGroupService.updateAttrGroupByIds(param);
        return Result.success("修改属性组状态",group);
    }

    /**
     * 根据id查询属性组
     * @param groupId 属性组id
     * @return 属性组信息
     * @throws Exception
     */
    @ApiOperation("根据id查询属性组")
    @ApiImplicitParam("属性组id")
    @ApiResponses({
            @ApiResponse(code = 200,message = "OK",response = AttrGroupDto.class)
    })
    @GetMapping("/findAttrGroupById")
    public Result findAttrGroupById(Integer groupId)throws Exception{
        AttrGroupDto group = attributeGroupService.findAttrGroupById(groupId);
        return Result.success("根据id查询属性组成功",group);
    }

    /**
     * 根据ids查询属性组
     * @param groupIds 属性组id集合
     * @return 属性组信息集合
     * @throws Exception
     */
    @ApiOperation("根据ids查询属性组")
    @ApiImplicitParam("属性组id集合")
    @ApiResponses({
            @ApiResponse(code = 200,message = "OK",response = AttrGroupDto.class)
    })
    @PostMapping("/findAttrGroupByIds")
    public Result findAttrGroupByIds(@RequestBody Set<Integer> groupIds)throws Exception{
        List<AttrGroupDto> groups = attributeGroupService.findAttrGroupByIds(groupIds);
        return Result.success("根据ids查询属性组成功",groups);
    }

    /**
     * 分页查询属性组
     * @param form 分页参数对象
     * @return 属性组分页对象
     * @throws Exception
     */
    @ApiOperation("分页查询属性组")
    @ApiImplicitParam("属性组名称")
    @ApiResponses({
            @ApiResponse(code = 200,message = "OK",response = AttrGroupDto.class)
    })
    @PostMapping("/findGroupPage")
    @Permission(roles = "超级管理员,管理员")
    public Result findGroupPage(@RequestBody FindConfigPageForm form)throws Exception{
        IPage<AttrGroupDto> groups = attributeGroupService.findGroupPage(BeanUtil.toBean(form, FindConfigPageParam.class));
        return Result.success("分页查询属性组成功",groups);
    }

}

